//
//  HMHomePictureView.swift
//  WeiBo
//
//  Created by Apple on 2017/6/3.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit

//  每项间距
private let itemMargin: CGFloat = 5
//  计算每个图片的宽度
private let itemWidth = (ScreenWidth - 2 * HomeCellMargin - 2 * itemMargin) / 3


//  MARK:   --首页配图
class HMHomePictureView: UICollectionView {
    
    
    //  图片数据源
    var picUrls: [HMPictureUrl]? {
        didSet {
            messageLabel.text = "\(picUrls?.count ?? 0)"
            //  根据个数计算配图大小
            calcSize(count: picUrls?.count ?? 0)
            //  重新加载数据
            self.reloadData()
        }
    }
    
    //  MARK:   --懒加载控件
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.yellow, fontSize: 30)
        return label
    }()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        //  设置大小
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //  设置最小间距
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  MARK:   --添加控件设置约束
    private func setupUI() {
        
        //  注册cell
        register(HMHomePictureViewCell.self, forCellWithReuseIdentifier: HomePictureCellIdentifier)
        //  设置数据源代理
        dataSource = self
        
        
        addSubview(messageLabel)
        messageLabel.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
    }

    //  根据图片个数计算配图大小
    private func calcSize(count: Int) {
        
        
        //  根据个数计算列数
        let cols = count > 3 ? 3 : count
        //  根据个数计算行数
        let rows = (count - 1) / 3 + 1
        
        //  配图的宽度
        let pictureViewWidth = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
        //  配图的高度
        let pictureViewHeight = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        
        let pictureViewSize = CGSize(width: pictureViewWidth, height: pictureViewHeight)
        //  更新配图大小
        self.snp.updateConstraints { (make) in
            //  更新约束的时候也要设置大小约束优先级
            make.size.equalTo(pictureViewSize).priority(.high)
        }
        
    }
    
}

//  MARK:   --UICollectionViewDataSource
extension HMHomePictureView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HomePictureCellIdentifier, for: indexPath) as! HMHomePictureViewCell
        //  获取显示图片的url
        cell.picUrl = picUrls![indexPath.item]
        return cell
    }
}


//  MARK:   --自定义配图cell
class HMHomePictureViewCell: UICollectionViewCell {
    //  MARK:   --懒加载控件
    private lazy var imageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
    
    //  设置图片
    var picUrl: HMPictureUrl? {
        didSet {
            if let imageUrl = picUrl?.thumbnail_pic {
                imageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        contentView.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
    }
    
}



